﻿using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Web.Configuration;

namespace msBsc.AppLogic.DataAlc
{
    public class CBizDjkOrderService : CustBizObject
    {
        private CDmDjkOrderService m_CDmDjkOrderService = new CDmDjkOrderService();
        private CDmDjkOrderService oDmDjkOrderService
        {
            get { return m_CDmDjkOrderService; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizDjkOrderService()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkOrderService.DJK_ORDER_SERVICE);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["ORDER_SERVICE_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        /// <summary>
        /// 添加预定服务信息
        /// </summary>
        /// <param name="orderType">预定类型（1、推荐线路，2、自助线路）</param>
        /// <param name="routeId">路线ID</param>
        /// <param name="vehicleId">车辆ID</param>
        /// <param name="djackId">地接客ID</param>
        /// <param name="serviceTime">接待时间</param>
        /// <param name="serviceAddr">接待地点</param>
        /// <param name="custName">买家姓名</param>
        /// <param name="custPhone">买家电话</param>
        /// <returns>string[]：index0:是否成功，index1:原因</returns>
        /// Author:刘魏
        /// Creation date:2015-9-28
        public string[] AddOrderServiceInfo(string orderType, string routeId, string vehicleId, string djackId, string serviceTime, string serviceAddr, string custName, string custPhone)
        {
            #region 验证信息
            if (string.IsNullOrWhiteSpace(serviceAddr))
            {
                return new string[] { "False", "接待地点不能为空" };
            }

            if (string.IsNullOrWhiteSpace(custName))
            {
                return new string[] { "False", "买家姓名不能为空" };
            }

            if (string.IsNullOrWhiteSpace(custPhone))
            {
                return new string[] { "False", "买家联系方式不能为空" };
            }

            DataSet dsvehicle, dsdjack;
            using (CBizVehicleInfo objCBizVehicleInfo = new CBizVehicleInfo())
            {
                dsvehicle = objCBizVehicleInfo.SelectById(vehicleId);

                if (dsvehicle == null
                    || dsvehicle.Tables[0].Rows.Count <= 0)
                {
                    return new string[] { "False", "预定车辆信息有误" };
                }
            }

            using (CBizCustomerBaseInfo objCBizCustomerBaseInfo = new CBizCustomerBaseInfo())
            {
                dsdjack = objCBizCustomerBaseInfo.SelectById(djackId);

                if (dsdjack == null
                    || dsdjack.Tables[0].Rows.Count <= 0)
                {
                    return new string[] { "False", "地接客信息有误" };
                }
            }
            #endregion

            #region 验证路线信息
            DataSet ds;

            CDbParmCollection parmCollection = new CDbParmCollection();

            parmCollection.Add(new CDbParm("routeId", ParameterDirection.Input, EDbType.String, 36, routeId));

            if (orderType == "1")
            {
                ds = this.DataObj.QueryBySQL("SELECT A.ROUTE_BASEINFO_ID ROUTE_ID,A.ROUTE_BASEINFO_NME ROUTE_NME,A.ROUTE_BASEINFO_DAYS ROUTE_DAYS FROM DJK_ROUTE_BASEINFO A WHERE A.ROUTE_BASEINFO_ID=@routeId", parmCollection);
            }
            else if (orderType == "2")
            {
                ds = this.DataObj.QueryBySQL("SELECT A.ORDER_ROUTE_ID ROUTE_ID,A.ORDER_ROUTE_NME ROUTE_NME,A.ORDER_ROUTE_DAYS ROUTE_DAYS FROM DJK_ORDER_ROUTE A WHERE A.ORDER_ROUTE_ID=@routeId", parmCollection);
            }
            else
            {
                return new string[] { "False", "线路类型有误，请确认选择了线路" };
            }
            #endregion

            #region 生成预定信息
            DataSet orderDs = new CDmDjkOrderService();

            DataRow dr = orderDs.Tables[0].NewRow();

            dr["ORDER_SERVICE_ID"] = this.DataObj.NewID();
            dr["ORDER_ROUTE_ID"] = routeId;
            dr["ORDER_ROUTE_NME"] = ds.Tables[0].Rows[0]["ROUTE_NME"];
            dr["ORDER_ROUTE_TYPE"] = orderType;
            dr["ORDER_ROUTE_DAYS"] = ds.Tables[0].Rows[0]["ROUTE_DAYS"];
            dr["ORDER_VEHICLE_ID"] = dsvehicle.Tables[0].Rows[0]["VEHICLE_INFO_ID"];
            dr["ORDER_VEHICLE_NME"] = dsvehicle.Tables[0].Rows[0]["VEHICLE_NAME"];
            dr["ORDER_VEHICLE_SEATS"] = dsvehicle.Tables[0].Rows[0]["VEHICLE_SEATS"];
            dr["ORDER_VEHICLE_TYPE"] = dsvehicle.Tables[0].Rows[0]["VEHICLE_TYPE"];
            dr["VEHICLE_TYPE_NME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.VehicleTypeEnum), dsvehicle.Tables[0].Rows[0]["VEHICLE_TYPE"]);
            dr["ORDER_VEHICLE_BRAND"] = dsvehicle.Tables[0].Rows[0]["VEHICLE_BRAND"];
            dr["VEHICLE_BRAND_NME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.VehicleBrandEnum), dsvehicle.Tables[0].Rows[0]["VEHICLE_BRAND"]);
            dr["ORDER_DJACK_ID"] = dsdjack.Tables[0].Rows[0]["CUSTOMER_BASEINFO_ID"];
            dr["ORDER_DJACK_NME"] = dsdjack.Tables[0].Rows[0]["CUSTOMER_BASEINFO_NAME"];
            dr["BEGIN_SERVICE_DT"] = serviceTime;
            dr["BEGIN_SERVICE_ADDR"] = serviceAddr;

            int serviceDays;
            if (!int.TryParse(ds.Tables[0].Rows[0]["ROUTE_DAYS"].ToString(), out serviceDays))
            {
                return new string[] { "False", "预定线路日程信息有误" };
            }

            decimal vehiclePrice;
            if (!decimal.TryParse(dsvehicle.Tables[0].Rows[0]["VEHICLE_PRICE_DAY"].ToString(), out vehiclePrice))
            {
                return new string[] { "False", "车辆价格有误" };
            }

            dr["TOTAL_CHARGE"] = serviceDays * vehiclePrice;
            dr["ORDER_COMMIT_DT"] = this.dtCurrDateTime;
            dr["BUYER_NME"] = custName;
            dr["BUYER_PHONE"] = custPhone;
            dr["CREATE_ID"] = msBsc.SvRef.UserInfo.Operator;
            dr["CREATE_DT"] = this.dtCurrDateTime;
            dr["ORDER_IS_VALID"] = 1;
            dr["ORDER_ROUTE_STATUS"] = 0;

            orderDs.Tables[0].Rows.Add(dr);
            #endregion

            this.Update(orderDs);

            return new string[] { "True", dr["ORDER_SERVICE_ID"].ToString() };
        }


        /// <summary>
        /// 根据预定服务编号查询预定服务详细信息
        /// </summary>
        /// <param name="serviceId">预定服务编号</param>
        /// <returns></returns>
        /// Author:刘魏
        /// Creation date:2015-9-29 
        public DataSet GetOrderServiceById(string serviceId)
        {

            DataSet ds = new DataSet();

            if (string.IsNullOrWhiteSpace(serviceId))
            {
                return ds;
            }

            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("serviceid", ParameterDirection.Input, EDbType.String, 36, serviceId));

            ds = this.DataObj.QueryBySP("DJK_SELECT_ORDER_SERVICE_FOR_VIEW", parmCollection);

            return ds == null ? new DataSet() : ds;
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="orderType">订单类型</param>
        /// <param name="routeId">路线id</param>
        /// <param name="vehicleId">车辆id</param>
        /// <param name="djackId">游客id</param>
        /// <returns></returns>
        ///  Author:蒲崇彬
        /// Creation date:2015-9-30
        public string[] AddServiceInfo(string orderType, string routeId, string vehicleId, string djackId)
        {
            
            #region 验证信息
            DataSet dsvehicle, dsdjack;
            using (CBizVehicleInfo objCBizVehicleInfo = new CBizVehicleInfo())
            {
                dsvehicle = objCBizVehicleInfo.SelectById(vehicleId);

                if (dsvehicle == null
                    || dsvehicle.Tables[0].Rows.Count <= 0)
                {
                    return new string[] { "False", "预定车辆信息有误" };
                }
            }

            using (CBizCustomerBaseInfo objCBizCustomerBaseInfo = new CBizCustomerBaseInfo())
            {
                dsdjack = objCBizCustomerBaseInfo.SelectById(djackId);

                if (dsdjack == null
                    || dsdjack.Tables[0].Rows.Count <= 0)
                {
                    return new string[] { "False", "地接客信息有误" };
                }
            }
            #endregion

            #region 验证路线信息
            DataSet ds;

            CDbParmCollection parmCollection = new CDbParmCollection();

            parmCollection.Add(new CDbParm("routeId", ParameterDirection.Input, EDbType.String, 36, routeId));

            if (orderType == "1")
            {
                ds = this.DataObj.QueryBySQL("SELECT A.ROUTE_BASEINFO_ID ROUTE_ID,A.ROUTE_BASEINFO_NME ROUTE_NME,A.ROUTE_BASEINFO_DAYS ROUTE_DAYS,COUNTRY_ID,COUNTRY_NME,CITY_ID,CITY_NME,DESTINATION_ID,DESTINATION_NME  FROM DJK_ROUTE_BASEINFO A WHERE A.ROUTE_BASEINFO_ID=@routeId", parmCollection);
            }
            else if (orderType == "2")
            {
                ds = this.DataObj.QueryBySQL("SELECT A.ORDER_ROUTE_ID ROUTE_ID,A.ORDER_ROUTE_NME ROUTE_NME,A.ORDER_ROUTE_DAYS ROUTE_DAYS,COUNTRY_ID,COUNTRY_NME,CITY_ID,CITY_NME,DESTINATION_ID,DESTINATION_NME FROM DJK_ORDER_ROUTE A WHERE A.ORDER_ROUTE_ID=@routeId", parmCollection);
            }
            else
            {
                return new string[] { "False", "线路类型有误，请确认选择了线路" };
            }
            #endregion

            #region 生成预定信息
            DataSet orderDs = new CDmDjkOrderService();

            DataRow dr = orderDs.Tables[0].NewRow();

            dr["ORDER_SERVICE_ID"] = this.DataObj.NewID();
            dr["ORDER_ROUTE_ID"] = routeId;
            dr["ORDER_ROUTE_NME"] = ds.Tables[0].Rows[0]["ROUTE_NME"];
            dr["ORDER_ROUTE_TYPE"] = orderType;
            dr["ORDER_ROUTE_DAYS"] = ds.Tables[0].Rows[0]["ROUTE_DAYS"];


            dr["COUNTRY_ID"] = ds.Tables[0].Rows[0]["COUNTRY_ID"];
            dr["COUNTRY_NME"] = ds.Tables[0].Rows[0]["COUNTRY_NME"];
            dr["CITY_ID"] = ds.Tables[0].Rows[0]["CITY_ID"];
            dr["CITY_NME"] = ds.Tables[0].Rows[0]["CITY_NME"];
            dr["DESTINATION_ID"] = ds.Tables[0].Rows[0]["DESTINATION_ID"];
            dr["DESTINATION_NME"] = ds.Tables[0].Rows[0]["DESTINATION_NME"];


            dr["ORDER_VEHICLE_ID"] = dsvehicle.Tables[0].Rows[0]["VEHICLE_INFO_ID"];
            dr["ORDER_VEHICLE_NME"] = dsvehicle.Tables[0].Rows[0]["VEHICLE_NAME"];
            dr["ORDER_VEHICLE_SEATS"] = dsvehicle.Tables[0].Rows[0]["VEHICLE_SEATS"];
            dr["ORDER_VEHICLE_TYPE"] = dsvehicle.Tables[0].Rows[0]["VEHICLE_TYPE"];
            dr["VEHICLE_TYPE_NME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.VehicleTypeEnum), dsvehicle.Tables[0].Rows[0]["VEHICLE_TYPE"]);
            dr["ORDER_VEHICLE_BRAND"] = dsvehicle.Tables[0].Rows[0]["VEHICLE_BRAND"];
            dr["VEHICLE_BRAND_NME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.VehicleBrandEnum), dsvehicle.Tables[0].Rows[0]["VEHICLE_BRAND"]);
            dr["ORDER_DJACK_ID"] = dsdjack.Tables[0].Rows[0]["CUSTOMER_BASEINFO_ID"];
            dr["ORDER_DJACK_NME"] = dsdjack.Tables[0].Rows[0]["CUSTOMER_BASEINFO_NAME"];

            int serviceDays;
            if (!int.TryParse(ds.Tables[0].Rows[0]["ROUTE_DAYS"].ToString(), out serviceDays))
            {
                return new string[] { "False", "预定线路日程信息有误" };
            }

            decimal vehiclePrice;
            if (!decimal.TryParse(dsvehicle.Tables[0].Rows[0]["VEHICLE_PRICE_DAY"].ToString(), out vehiclePrice))
            {
                return new string[] { "False", "车辆价格有误" };
            }

            dr["TOTAL_CHARGE"] = serviceDays * vehiclePrice;
            dr["ORDER_COMMIT_DT"] = this.dtCurrDateTime;

            dr["CREATE_ID"] = msBsc.SvRef.UserInfo.Operator;
            dr["CREATE_DT"] = this.dtCurrDateTime;
            dr["ORDER_IS_VALID"] = 1;
            dr["ORDER_ROUTE_STATUS"] = 0;

            orderDs.Tables[0].Rows.Add(dr);
            #endregion

            this.Update(orderDs);

            return new string[] { "True", dr["ORDER_SERVICE_ID"].ToString() };
        }
    }
}
